<?php

/**
 * payperiod actions.
 *
 * @package    virtualstore
 * @subpackage payperiod
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class payperiodActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    //echo "Use Id: " . $this->getUser()->getGuardUser()->getId();

//    $this->pay_periods = Doctrine::getTable('PayPeriod')
//      ->createQuery('a')
//      ->execute();
       $items_x_page = 10;
        $this->pager = new sfDoctrinePager('PayPeriod', $items_x_page); // Table, items per page
        $this->pager->getQuery()->from('PayPeriod p')->orderBy('p.startDate ASC');

        //$this->pager->getQuery(); // items query
        $this->pager->setPage($request->getParameter('page', 1)); // actual page
        $this->pager->init();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->pay_period = Doctrine::getTable('PayPeriod')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->pay_period);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new PayPeriodForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new PayPeriodForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

   public function executeGetTimeCards(sfWebRequest $request)
  {
     //$id = $request->getParameter('id');
     $tcid = $request->getParameter('time_card_id');
     $this->timeCards = Doctrine::getTable('TimeCardDay')
             ->findByTimeCardId($tcid);
//     $this->timeCards = Doctrine::getTable('TimeCardDay')
//             ->findByIdAndTimeCardId(array($id,$tcid));

   }
  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($pay_period = Doctrine::getTable('PayPeriod')->find(array($request->getParameter('id'))), sprintf('Object pay_period does not exist (%s).', $request->getParameter('id')));
    $this->form = new PayPeriodForm($pay_period);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($pay_period = Doctrine::getTable('PayPeriod')->find(array($request->getParameter('id'))), sprintf('Object pay_period does not exist (%s).', $request->getParameter('id')));
    $this->form = new PayPeriodForm($pay_period);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($pay_period = Doctrine::getTable('PayPeriod')->find(array($request->getParameter('id'))), sprintf('Object pay_period does not exist (%s).', $request->getParameter('id')));
    $pay_period->delete();

    $this->redirect('payperiod/index');
  }
  public function executeCreateTimeCards(sfWebRequest $request){
      $pay_period = Doctrine::getTable('PayPeriod')->find(array($request->getParameter('id')));
      $pay_period->createTimeCards();
      $this->redirect('payperiod/show?id='.$pay_period->getId());
  }
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $pay_period = $form->save();

      $this->redirect('payperiod/edit?id='.$pay_period->getId());
    }
  }
}
